package com.plum.admin.config.security.filter;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.plum.admin.common.annotation.Log;
import com.plum.admin.common.constant.Constant;
import com.plum.admin.common.enums.LogType;
import com.plum.admin.common.util.ResponseUtil;
import com.plum.admin.common.util.SecurityUtil;
import com.plum.admin.common.vo.TokenUser;
import com.plum.admin.config.security.SecurityUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/8/13 14:34
 */
@Component
@Slf4j
public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Autowired
    private SecurityUtil securityUtil;
    @Override
    @Log(title = "登录成功",type = LogType.LOGIN)
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        SecurityUserDetails userDetails = (SecurityUserDetails) authentication.getPrincipal();
        String username=userDetails.getUsername();
        String token = securityUtil.getToken(username);
        ResponseUtil.out(response,ResponseUtil.resultMap(200,"登录成功", Map.of("token",token)));
    }
}
